﻿using System;
using System.Collections.Generic;
using System.Text;
using My.GenCode.Entities;
using My.GenCode.Interface;
using My.GenCode.Templates;
using System.IO;
using System.Web.Script.Serialization;

namespace CmdClient
{
    class Program
    {
        static void Main(string[] args)
        {
            string tplPath = @"D:\mycode\develop\MAF\code\My.GenCode\Service\Template\";
            string entitiesFilename= @"D:\mycode\develop\MAF\code\My.GenCode\CmdClient\Data\Blog.txt";
            string outputPath = @"D:\temp\Code\";
            Program p = new Program();
            p.GenCodeManager(tplPath, entitiesFilename, outputPath);
        }


        public void GenCodeManager(string tplPath,string entitiesFilename,string outputPath)
        {
            //模板词典
            Dictionary<string, string> tplDict = new Dictionary<string, string>();
            tplDict["Entity"] = GetTpl(tplPath,@"0 Entity.cs.tpl");
            tplDict["Interface"] = GetTpl(tplPath,@"1 Interface.cs.tpl");
            tplDict["Service"] = GetTpl(tplPath,@"2 Service.cs.tpl");
            tplDict["Safe"] = GetTpl(tplPath,@"3 Safe.cs.tpl");
            tplDict["Dao"] = GetTpl(tplPath,@"4 Dao.cs.tpl");
            tplDict["SqlMap"] = GetTpl(tplPath,@"5 Sql.xml.tpl");
            tplDict["Handler"] = GetTpl(tplPath,@"6 Handler.cs.tpl");
            tplDict["AjaxPage"] = GetTpl(tplPath,@"7 AjaxPage.aspx.tpl");
            tplDict["CreatePage"] = GetTpl(tplPath,@"8 CreatePage.aspx.tpl");
            tplDict["ListPage"] = GetTpl(tplPath,@"9 ListPage.aspx.tpl");
            tplDict["DetailPage"] = GetTpl(tplPath,@"a DetailPage.aspx.tpl");
            tplDict["DbScript"] = GetTpl(tplPath,@"-1 DbScript.sql.tpl");

            //实体列表
            IList<EntityInfo> entityList = ParseEntities(entitiesFilename);

            IList<CodeTemplate> cts = new List<CodeTemplate>();
            //创建单个实体对应的对象
            foreach (EntityInfo info in entityList)
            {
                cts.Add( new GenEntityClass(tplDict["Entity"],info,"业务数据结构(实体)") );
                cts.Add( new GenInterfaceClass(tplDict["Interface"],info,"业务操作接口(系统接口)") );

                cts.Add( new GenSafeClass(tplDict["Safe"],info,"业务安全层") );
                cts.Add( new GenDaoClass(tplDict["Dao"],info,"数据库访问层") );
                cts.Add( new GenSqlMap(tplDict["SqlMap"],info,"IBatis的Sql映射") );

                cts.Add( new GenHandlerClass(tplDict["Handler"],info,"Web处理器，用户界面与服务程序交互的第一个对象") );
                
                cts.Add( new GenCreatePage(tplDict["CreatePage"],info,"创建页面") );
                cts.Add( new GenListPage(tplDict["ListPage"],info,"列表页面+删除页面") );
                cts.Add( new GenDetailPage(tplDict["DetailPage"],info,"详细页面+修改页面") );
            }

            // 创建多个实体对应的对象
            cts.Add( new GenServiceClass(tplDict["Service"], entityList,"业务门面类") );
            cts.Add( new GenAjaxPage(tplDict["AjaxPage"],entityList,"Ajax请求处理页面，或许会将来会交给IHttpHadler接口处理") );
            cts.Add( new GenDbScript(tplDict["DbScript"],entityList,"数据库表创建脚本") );

            // 生成代码
            foreach( CodeTemplate ct in cts )
            {
                ct.Generate(outputPath);
            }
        }

        // 取得模板
        private string GetTpl(string path,string filename)
        {
            return File.ReadAllText(Path.Combine(path, filename));
        }

        // 解析输入的实体文件
        private IList<EntityInfo> ParseEntities(string filename)
        {
            string json = File.ReadAllText(filename);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            IList<EntityInfo> list = jss.Deserialize<IList<EntityInfo>>(json);
            return list;
        }
    }
}
